package com.herench.bunch.commons.utils;

import ca.uhn.hl7v2.DefaultHapiContext;
import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.HapiContext;
import ca.uhn.hl7v2.model.AbstractMessage;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.segment.MSH;
import ca.uhn.hl7v2.parser.ParserConfiguration;
import ca.uhn.hl7v2.parser.PipeParser;
import com.herench.bunch.commons.support.RequestMessage;
import oracle.jdbc.OracleDriver;
import org.springframework.util.Assert;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.*;

public class MessageHelper {

    public static RequestMessage decoration(RequestMessage requestMessage) throws Exception {
        String originalMsg = requestMessage.getOriginalMsg();
        Assert.notNull(originalMsg, "originalMsg 为空.");
        String decodeString = URLDecoder.decode(originalMsg, "UTF-8");
        AbstractMessage hl7Message = null;
        HapiContext context = new DefaultHapiContext();
        PipeParser pipeParser = context.getPipeParser();
        ParserConfiguration parserConfiguration = context.getParserConfiguration();
        parserConfiguration.setValidating(false);
        hl7Message = (AbstractMessage)pipeParser.parse(decodeString);
        MSH msh = (MSH)hl7Message.get("MSH");
        String msgCode = msh.getMsh11_ProcessingID().getPt1_ProcessingID().getValue();
        requestMessage.setHl7Message(hl7Message);
        requestMessage.setMsgCode(msgCode);
        requestMessage.setId(msh.getMsh11_ProcessingID().getProcessingMode().getValue());
        requestMessage.setSender(msh.getMsh3_SendingApplication().getHd1_NamespaceID().getValue());
        return requestMessage;
    }

    public static String encode(Message hl7Message) {
        HapiContext context = new DefaultHapiContext();
        PipeParser pipeParser = context.getPipeParser();
        ParserConfiguration parserConfiguration = context.getParserConfiguration();
        parserConfiguration.setValidating(false);
        try {
            String res = pipeParser.encode(hl7Message);
            return URLEncoder.encode(res, "UTF-8");
        } catch (HL7Exception e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String result(String sql){

        ResultSet resultSet = null;
        String hisDbUrl="jdbc:oracle:thin:@172.16.231.60:1521/ora10g";
        String hisUsername="dbhis";
        String hisPassword="dbhis";
        String queueNo = null;

        try {
            Class.forName(OracleDriver.class.getName());
            Connection connection = DriverManager.getConnection(hisDbUrl,hisUsername,hisPassword);
            PreparedStatement stmt =  connection.prepareStatement(sql);
             resultSet = stmt.executeQuery();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            while(resultSet.next()){
                queueNo = resultSet.getString("SORTNUM_DESC");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return queueNo;
    }

}
